<template>
  <div>
    <!-- 搜索 -->
    <!-- 预授权级别维护 -->
    <TopFilter ref="topFilter" :list="topFilterInit" :cols="3" :collapse="collapse" labelWidth="120" @filterChange="changeHandle"></TopFilter>
    <!-- table -->
    <FilterTable ref="table" :columns="columns" :params="fetchParams" :fetchapi="fetchapi" :datakey="'records'" :onColumnsChange="columns => (this.columns = columns)" :onRowSelectChange="selectChange" columnsRef="AuthApplicationlgr" :is-select-column="false">
      <template slot="controls" slot-scope="props">
        <!--<multiuse-button size="small" type="primary" @click="deleteMore()">批量删除</multiuse-button>-->
        <multiuse-button size="small" type="primary" icon="el-icon-plus" @click="add()">新增</multiuse-button>
      </template>
    </FilterTable>
    <BaseDialog width="30%" :visible.sync="visibleEdit" :title="dialogTitle" destroyOnClose :containerStyle="{ height: 'calc(100% - 70px)', overflow: 'auto', paddingBottom: '70px' }">
      <diaLogForm :getLevelData="levelData" :rowData="rowData" :type="type" @close="closeDialog" @saveDialog="saveDialog" />
    </BaseDialog>
  </div>
</template>

<script>
import { getListEmp, deleteEmp, getListByPage } from '@/api/claim/dealer/AuthMaintenance';
import { factoryEmployeeSearch } from '@/api/common/auth/dealerStaff';
import diaLogForm from './diaLogForm.vue';
import { dictionary } from '@/utils/dictMixin';
export default {
  name: "index",
  components: {
    diaLogForm
  },
  mixins: [dictionary],
  data() {
    return {
      topFilterInit: this.topFilterInitFun(),
      fetchParams: { xhrAbort: false }, //ipa 接口
      columns: this.creatHeader(),
      selectedRows: [], //选中
      collapse: true,
      fetchapi: getListEmp,
      visibleEdit: false,
      rowData: {},
      dialogTitle: '',
      type: '',
      levelData: ''
    }
  },
  created() { },
  methods: {
    //获取预授权级别
    getLevelData() {
      let arr = []
      let levelDataArr = []
      getListByPage().then(res => {
        if (res.data) {
          res.data.forEach((item, i) => {
            arr.push({ text: item.name, value: item.name });
            levelDataArr.push({ text: `${item.name}-${this.createDictText(item.authType,'5508')}`, value: item.id });
          })
          this.levelData = levelDataArr;
        }
      })
      return arr
    },
    //批量删除
    deleteMore() {

    },
    //搜索数据
    topFilterInitFun() {
      return [
        { type: 'SELECT', label: '授权级别', fieldName: 'name', placeholder: '请输入预授权级别...', itemList: this.getLevelData(), initialValue: '' },
        { type: 'INPUT', label: '员工姓名', fieldName: 'empName', placeholder: '请输入员工姓名...', initialValue: '' },
        { type: 'SELECT', label: '预授权类型', fieldName: 'authType', placeholder: '请选择预授权类型...', itemList: this.createDictList('5508'), initialValue: '' },
      ];
    },
    //搜索获取数据
    changeHandle(val) {
      this.fetchParams = Object.assign({}, this.fetchParams, val, { xhrAbort: false });
    },
    //table表头和数据源
    creatHeader() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        { title: '员工编号', dataIndex: 'empCode', },
        { title: '员工姓名', dataIndex: 'empName', },
        { title: '岗位', dataIndex: 'position', },
        { title: '预授权级别', dataIndex: 'name', },
        {
          title: '预授权类型', dataIndex: 'authType',
          filterItems: this.createDictList('5508'),
          dictItems: this.createDictList('5508'),
          render: props => {
            return <span>{this.createDictText(props.row.authType, '5508')}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 100,
          render: props => {
            return (
              <div style="text-align:center">
                <multiuse-button size="mini" type="text" onClick={() => this.edit(props.row)} >
                  编辑 </multiuse-button>
                <el-button size="mini" type="text" onClick={() => this.deleteItem(props.row)}>
                  删除
                </el-button>
              </div>
            );
          }
        },
      ];
    },
    //选中行
    selectChange(rows) {
      this.selectedRows = rows;
      console.log(this.selectedRows);
    },
    editclose() {
      this.visibleEdit = false;
    },
    edit(val) {
      this.rowData = val;
      console.log('数据2', this.rowData)
      this.visibleEdit = true;
      this.dialogTitle = '预授权编辑';
      this.type = 'edit'
    },
    add() {
      this.rowData = {}
      this.dialogTitle = '预授权新增';
      this.visibleEdit = true;
      this.type = 'add'
    },
    deleteItem(val) {
      // this.deleteList.push(val);
      // let array = [val];
      // this.$refs.table.EXECUTE_DELETE(array);
      // this.$notify({
      //   title: '成功',
      //   message: '删除成功',
      //   type: 'success'
      // });
      this.$confirm('确定删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const res = await deleteEmp(val.id)
        if (res.errMsg) {
          this.$notify.error({ title: '提示信息', message: res.errMsg || '删除失败!' });
        } else {
          this.$notify.success({ title: '提示信息', message: '删除成功!' });
          // 重新加载
          this.fetchParams = { ...this.fetchParams };
        }
      }).catch(() => {
        // this.$notify.info({title: '提示信息',message:'已取消删除'});
      });
    },
    async closeDialog() {
      // this.action.visible = false;
      this.visibleEdit = false;
      const [err, data] = await this.$refs.topFilter.GET_FORM_DATA();
      this.fetchParams = { ...this.fetchParams, ...data };
    },
    async saveDialog() {
      const [err, data] = await this.$refs.topFilter.GET_FORM_DATA();
      this.fetchParams = { ...this.fetchParams, ...data };
    }
  }
}
</script>

<style scoped>
</style>
